1765N - Number Reduction - CodeForces Solution


greedy *1500

Please click on ads to support us..

Python Code:

import bisect
def rem(ss,kk):
    ret=[]
    for c in ss:
        while (len(ret)>0 and kk and ret[-1]>c):
            ret.pop();kk-=1
        ret.append(c)
    while len(ret)>0 and kk:
        ret.pop();kk-=1
    return ''.join(ret)
t=int(input())
for tc in range(t):
    s=input()
    n=len(s)
    k=int(input())
    i=0;m=s[0]
    for j in range(1,k+1):
        if '0'<s[j]<m:m=s[j];i=j
    print(m+rem(s[i+1:],k-i))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
/////////////////////////////////////////DEFINE//////////////////////////////////////////

#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define MAXN                      10000001

typedef long long ll;
typedef vector<long long> vll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef map<long long, long long> mll;
typedef vector<pair<long, long>> vpll;

////////////////////////////////////////CONST////////////////////////////////////////////

const int inf = 0x3f3f3f3f;
const int maxn = 2e6 + 6;
const double eps = 1e-8;
const int mod = 1000000007;
const int N = 1e7;

///////////////////////////////////////FUNCTION//////////////////////////////////////////

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
bool cmp(int c, int d) { return c > d;}
void solve()
{
     string s;cin>>s;
     ll k;cin>>k;
     ll n=s.length();
     string ans="";
     for(ll i=0;i<n;i++)
     {
         if(s[i]=='0')
         {
            while(ans.size()>1 && k>0 && ans.back()!='0')
            {
                ans.pop_back();
                k--;
            }
         }
         else{
            while(ans.size()>0 && k>0 && ans.back()>s[i])
            {
                ans.pop_back();
                k--;
            }
            if(ans.size()>0 && k>0 && ans.back()=='0')
            {
                if(ans[0]>s[i] && k>=ans.size())
                {
                    k-=ans.size();
                    ans="";
                }
            }
         }
         ans.pb(s[i]);
         }

         while(k--)
          {
            ans.pop_back();
          }
          cout<<ans<<'\n';
     }
      
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
ll t;
cin>>t;
while(t--)
solve();
return 0;
}


Comments

Submit
0 Comments
More Questions

151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother